<!--
 * @Author: your name
 * @Date: 2021-05-17 15:26:20
 * @LastEditTime: 2021-05-17 18:50:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-demo\src\components\Carousel.vue
-->
<template>
  <div class="wrap"  @mouseout="nextCarousel()" @mouseover="clearSetintervalItem()">
    <div class="banner">
      <ul
        :style="{
          width: 100 * imgSrc.length + '%',
          transform: `translate(${-(100 / imgSrc.length) * index}%,0)`,
        }"
      >
        <li
          :style="{ width: 100 / imgSrc.length + '%' }"
          v-for="(it, i) in imgSrc"
          :key="it + i"
        >
          <img :src="it" />
        </li>
      </ul>
    </div>
    <div class="btn">
      <ul>
        <li
          :class="{ current: i === index }"
          v-for="(it, i) in imgSrc"
          :key="it + i"
          @click="headleClickBtn(i)"
        ></li>
      </ul>
    </div>
    <div class="arr">
      <ul>
        <li @click="prevIndex()">&lt;</li>
        <li @click="nextIndex()">&gt;</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    imgSrc: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      index: 0,
      setintervalItem: null,
    };
  },
  methods: {
    headleClickBtn(i) {
      this.index = i;
    },
    nextCarousel() {
      this.clearSetintervalItem();
      this.setintervalItem = setInterval(() => {
      this.nextIndex();
      }, 2000);
    },
    prevIndex() {
      if (this.index <= 0) {
        return (this.index = this.imgSrc.length - 1);
      } else {
        return this.index--;
      }
    },
    nextIndex() {
      if (this.index >= this.imgSrc.length - 1) {
        return (this.index = 0);
      } else {
        return this.index++;
      }
    },
    clearSetintervalItem() {
      clearInterval(this.setintervalItem);
    },
  },
  mounted() {
    this.nextCarousel();
  },
};
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 600px;
  position: relative;
}
.wrap .banner {
  width: 100%;
  height: 600px;
  margin: 0 auto;
  overflow: hidden;
}

.wrap .banner ul {
  width: 200%;
  height: 600px;
  flex-wrap: nowrap;
  display: flex;
  justify-content: flex-start;
  transition: all 0.5s;
}

.wrap .banner ul li {
  width: 50%;
  height: 600px;
  list-style: none;
}

.wrap .banner ul li img {
  width: 100%;
  height: 100%;
}

.wrap .btn {
  position: absolute;
  bottom: 40px;
  width: 100%;
}

.wrap .btn ul {
  display: flex;
  justify-content: center;
  height: 20px;
}

.wrap .btn ul li {
  list-style: none;
  height: 20px;
  width: 20px;
  border-radius: 50%;
  background: #333333b6;
  margin: 0 10px;
  cursor: pointer;
}

.wrap .btn ul li.current {
  background: #3eaf7c;
}

.wrap .arr {
  width: 100%;
  position: absolute;
  top: 50%;
  left: 0;
  user-select:none
}

.wrap .arr ul {
  display: flex;
  justify-content: space-between;
  list-style-type: none;
}

.wrap .arr ul li {
  font-size: 60px;
  color: #fff;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  background: #333333b6;
  cursor: pointer;
}

.wrap .arr ul li:hover {
  background: #3eaf7c;
}
</style>